/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_priority_queue
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>

#include "jh_priority_queue.h"
/************************************************************************/
/*                                                                      */
/************************************************************************/
//自定义数据结构
typedef struct {
    int data;       //数据
    int priority;   //优先级
} my_priority_data_t;

int compare(const void * a, const void * b)
{
    //将优先级值最小，优先级最高的数据放在队头
    return ( ((my_priority_data_t*)a)->priority < ((my_priority_data_t*)b)->priority );
}

int main(int argc, char* argv[])
{
    //定义数据结构和用于存放数据的数组
    jh_priority_queue_t pqueue;
    my_priority_data_t buf[10];
    
    //优先队列初始化
    jh_priority_queue_init(&pqueue, buf, 10, sizeof(my_priority_data_t), compare);
    
    //定义要用于传入的数据
    my_priority_data_t items[] = {{81, 6}, {13, 3}, {16, 5}, {38, 4}, {49, 1}, {67, 2}};
    my_priority_data_t val;
    int i;
    
    //数据入队
    printf("\npush:\n");
    for(i = 0; i < sizeof(items)/sizeof(my_priority_data_t); i++) {
        if(!jh_priority_queue_push(&pqueue, &items[i]))
            printf("data %d priority %d\n", items[i].data, items[i].priority);
    }
    printf("\n");
    
    //查看当前的状态
    printf("count : %d empty : %d full : %d\n", jh_priority_queue_count(&pqueue), jh_priority_queue_is_empty(&pqueue), jh_priority_queue_is_full(&pqueue));
    
    //数据出队
    printf("\npop:\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_priority_queue_pop(&pqueue, &val))
            printf("data %d priority %d\n", val.data, val.priority);
    }
    printf("\n");
    
    //查看当前的状态
    printf("count : %d empty : %d full : %d\n", jh_priority_queue_count(&pqueue), jh_priority_queue_is_empty(&pqueue), jh_priority_queue_is_full(&pqueue));

    return 0;
}